package com.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bean.Goods;
import com.serve.GoodsService;
import com.serveImple.GoodsServiceImpl;

/**
 * Servlet implementation class GoodsServlet
 */
@WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		/*String name=request.getParameter("name");*/
		String descripe=request.getParameter("descripe");
		String price=request.getParameter("price");
		String store=request.getParameter("store");
		String address=request.getParameter("address");
		String gpid=request.getParameter("gpid");
		//字符串转成int
		int pric= Integer.parseInt(price);
		int sto=Integer.parseInt(store);			
		Goods goods=new Goods();
		goods.setG_name(request.getParameter("name"));//和下面同样效果
		/*goods.setG_name(name);*/
		goods.setG_price(pric);
		goods.setStore(sto);
		goods.setG_address(address);
		goods.setGp_id(gpid);
		goods.setDescripe(descripe);
		
		GoodsService goodsservice=new GoodsServiceImpl();
		int i =goodsservice.addGoods(goods);
		if(i!=1){
			request.setAttribute("jie", 1);
			request.setAttribute("jieguo", i);
			System.out.println("添加失败");
			request.getRequestDispatcher("easybuy/goods.jsp").forward(request, response);
		}else{
			request.setAttribute("jie", 1);
			request.setAttribute("jieguo", i);
			System.out.println("添加成功");
			request.getRequestDispatcher("easybuy/goods.jsp").forward(request, response);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}
